/**
 * @(#)com.framework.web.wechat.controller.WeChatGoodsController
 *
 * Copyright (c) 2014-2018 一抹风光
 *
 * DESC:
 *
 */
package com.framework.web.wechat.controller;

import com.framework.core.util.Persistence;
import com.framework.core.util.StringsToListUtil;
import com.framework.web.bus.domain.GoodsInfo;
import com.framework.web.bus.domain.GoodsType;
import com.framework.web.bus.service.GoodsInfoService;
import com.framework.web.bus.service.GoodsTypeService;
import com.framework.web.common.domain.BusinessConstants;
import com.framework.web.common.domain.Constants;
import com.framework.web.wechat.domain.WeChatRequest;
import com.framework.web.wechat.domain.WeChatResInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fusf
 * @version 1.0  2016/9/11
 */
@Controller
@RequestMapping("/weChat/goods")
public class WeChatGoodsController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private GoodsInfoService goodsInfoService;

    /**
     * 商品类别列表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getGoodsTypeList", method = RequestMethod.GET)
    @ResponseBody
    public Object getGoodsTypeList(HttpServletRequest request) throws Exception {
        WeChatResInfo resInfo = new WeChatResInfo();
        Map<String, Object> map = new HashMap<>();
        List<GoodsType> goodsTypeList = goodsTypeService.findAllByStatus();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (GoodsType goodsType : goodsTypeList) {
            Map<String, Object> map_object = new HashMap<>();
            List<GoodsInfo> goodsInfoList = goodsInfoService.findByGoodsTypeIdOrderBySalesVolume(goodsType.getId());
            if (goodsInfoList != null && goodsInfoList.size() > 0) {
                GoodsInfo goodsInfo = goodsInfoList.get(0);
                map_object.put("preferentical", goodsInfo.getCurrentPrice());// 优惠价
                map_object.put("salesNumber", goodsInfo.getSalesVolume());//销量
                if (goodsInfo.getIsPost() == 0) {
                    map_object.put("postDescription", goodsInfo.getPostDesc());//快递描述
                } else {
                    map_object.put("postDescription", "");
                }
            } else {
                map_object.put("preferentical", 0.0);// 优惠价
                map_object.put("salesNumber", 0);//销量
                map_object.put("postDescription", "");
            }
            map_object.put("typeId", goodsType.getId());
            map_object.put("title", goodsType.getTitle());
            map_object.put("description", goodsType.getDescription());
            map_object.put("content", goodsType.getContent());
            mapList.add(map_object);
        }
        map.put("status", BusinessConstants.WECHAT_STATUS.SUCCESS);
        map.put("msg", "成功");
        map.put("infoList", mapList);
        resInfo.setResult(map);
        return resInfo;

    }

    /**
     * 商品列表
     *
     * @param requestInfo
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    @ResponseBody
    public Object getGoodsList(WeChatRequest requestInfo, HttpServletRequest request) throws Exception {
        WeChatResInfo resInfo = new WeChatResInfo();
        Map<String, Object> map = new HashMap<>();
        if (requestInfo.getTypeId() == null) {
            map.put("status", BusinessConstants.WECHAT_STATUS.E50010002);
            map.put("msg", "商品分类不存在");
            resInfo.setResult(map);
            return resInfo;
        }
        GoodsType goodsType = goodsTypeService.findById(requestInfo.getTypeId());
        if (goodsType == null) {
            map.put("status", BusinessConstants.WECHAT_STATUS.E50010002);
            map.put("msg", "商品分类不存在");
            resInfo.setResult(map);
            return resInfo;
        }
        //商品类别相关
        map.put("typeId", goodsType.getId());
        map.put("title", goodsType.getTitle());
        map.put("description", goodsType.getDescription());
        map.put("content ", goodsType.getContent());//商品类别的描述

        //商品相关
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<GoodsInfo> goodsInfoList = goodsInfoService.findByGoodsTypeId(requestInfo.getTypeId());
        for (GoodsInfo goodsInfo : goodsInfoList) {
            Map<String, Object> map_object = new HashMap<>();
            map_object.put("goodsId", goodsInfo.getId());//商品Id
            map_object.put("image", Persistence.getValue(Constants.FILEIP) + StringsToListUtil.getFistImage(StringsToListUtil.getFistImage(goodsInfo.getImg()), "@"));//商品图片
            map_object.put("price", goodsInfo.getPrice());//当前价格
            map_object.put("preferentical", goodsInfo.getCurrentPrice());//优惠价格
            map_object.put("description", goodsInfo.getDescription());//商品描述
            map_object.put("freightPrice", "0");//运费价格  TODO
            map_object.put("freightFree", goodsInfo.getIsPost());//0免运费 1 不免运费
            if (goodsInfo.getIsPost() == 0) {
                map_object.put("postDescription", goodsInfo.getPostDesc());//快递描述
            } else {
                map_object.put("postDescription", "");
            }
            map_object.put("shopName", Persistence.getValue(Constants.SHOP_NAME));//店铺名称
            map_object.put("buyers", goodsInfo.getSalesVolume());//购买人数 ,销量
            map_object.put("reamining", goodsInfo.getStock()); //剩余数量
            mapList.add(map_object);
        }
        map.put("infoList", mapList);
        map.put("status", BusinessConstants.WECHAT_STATUS.SUCCESS);
        map.put("msg", "成功");
        resInfo.setResult(map);
        return resInfo;

    }

    /**
     * 商品详情
     *
     * @param requestInfo
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/goodsDetail", method = RequestMethod.GET)
    @ResponseBody
    public Object getGoodlist(WeChatRequest requestInfo, HttpServletRequest request) throws Exception {
        WeChatResInfo resInfo = new WeChatResInfo();
        Map<String, Object> map = new HashMap<>();
        GoodsInfo goodsInfo = goodsInfoService.findById(requestInfo.getGoodsId());
        if (goodsInfo == null) {
            map.put("status", BusinessConstants.WECHAT_STATUS.E50010002);
            map.put("msg", "商品不存在");
            resInfo.setResult(map);
            return resInfo;
        }

        map.put("title", goodsInfo.getTitle());
        map.put("description", goodsInfo.getDescription());
        map.put("goodsId", goodsInfo.getId());//商品Id
        map.put("image", Persistence.getValue(Constants.FILEIP) + StringsToListUtil.getFistImage(goodsInfo.getImg(), "@"));//图片
        map.put("price", goodsInfo.getPrice());//当前价格
        map.put("preferentical", goodsInfo.getCurrentPrice());//优惠价格
        map.put("freightPrice", "0");//运费价格  TODO
        map.put("freightFree", goodsInfo.getIsPost());//0免运费 1 不免运费
        if (goodsInfo.getIsPost() == 0) {
                map.put("postDescription", goodsInfo.getPostDesc());//快递描述
            } else {
                map.put("postDescription", "");
            }
        map.put("shopName", Persistence.getValue(Constants.SHOP_NAME));//店铺名称
        map.put("shopDescription", Persistence.getValue(Constants.SHOP_DESCRIPTION));//店铺描述
        map.put("buyers", goodsInfo.getSalesVolume());//购买人数 ,销量
        map.put("reamining", goodsInfo.getStock()); //剩余数量

        List<String> list = new ArrayList<String>();  //TODO
        list.add("138*****764");
        list.add("138*****765");
        list.add("138*****766");
        map.put("buyerPhones", list);
        map.put("status", BusinessConstants.WECHAT_STATUS.SUCCESS);
        map.put("msg", "成功");
        resInfo.setResult(map);
        return resInfo;

    }


}
